
local table = table
local page_data = require 'types.shop.page_data'
local page = class('page')
page.type = 'shop_page'
page.name = nil

--创建页面
function page:ctor(name)
    local data = page_data[name]
    if not data then

    end
	return function(list)
		local page = ac.shop.readList(list)
		ac.shop.all_pages[name] = page

		page.name = name

		setmetatable(page, ac.shop.page)

		return page
	end
end

--获取页面上某个格子的值
function page:getValue(y, x)
	return self[x] and self[x][y]
end

function page:get_name(x, y)
	local value = self:getValue(x, y)
	if value then
		return value:sub(2)
	end
end

function page:getType(x, y)
	local value = self:getValue(x, y)
	if value then
		return value:sub(1, 1)
	end
end

--解析页面
function ac.shop.readList(list)
	local page = {}

	--解析每一行
	for line in list:gmatch '[^\r\n]+' do
		local line_data = {}
		for name in line:gmatch '%S+' do
			table.insert(line_data, name)
		end
		if #line_data ~= 0 then
			table.insert(page, line_data)
		end
	end

	return page

end

--获取页面
--	页面名称
function ac.shop.__index:getPage(name)
	return self.all_pages and self.all_pages[name] or shop.all_pages[name]
end

--设置页面
--	页面名称
--	@设置具体页面内容
	--	页面内容表
function ac.shop.__index:setPage(name)
	return function(page)
		if not self.all_pages then
			self.all_pages = {}
		end

		self.all_pages[name] = page
	end
end

function ac.shop.initPage()
	ac.shop.all_pages = {}
	
	return ac.shop
end

return ac.shop.initPage()